//
//  OauthModel.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/2.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit

class OauthModel: NSObject,NSCoding{
    /*
    "access_token" = "2.00euKSgCAGnlAD75bb86223fO_hgHC";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 2457203632;
    */
    
    var access_token: String?
    
    var expires_in: NSTimeInterval = 0 {
        
        didSet {
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    var uid: String?
    
    var expires_Date: NSDate?
    
    init(dict: [String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //MARK: --用户的信息数据
    
    var screen_name: String?
    
    var profile_image_url: String?
    
    override var description:String {
        
        let keys = ["screen_name", "profile_image_url", "expires_Date"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
    }
    
}

extension OauthModel {
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
        aCoder.encodeObject(uid, forKey: "uid")
    }
    
    func saveIn() {
        
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
        
        let filePath = (path! as NSString).stringByAppendingPathComponent("cdw.plist")
        
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
    }
    
    
    class func getUserInfo() -> OauthModel? {
        
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
        
        let filePath = (path! as NSString).stringByAppendingPathComponent("cdw.plist")
        
        let account = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? OauthModel
        //对过期的处理,根据储存的时间和现在的时间进行比较来判断是否返回account,即access_token的值;
        if let oldDate = account?.expires_Date {
            
            if oldDate.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                
                return account
                
            } else {
                
                return nil
            }
        }
        //第一次的时候oldDate没有值 所以只能在这里进行返回控制,让其进行授权;
        return nil
    }
}